package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Map> findAllMenu() {
        List<Menu> menuList = findAll();
        // 构建parent_Id 为0 的数据
        return findMenuListByParentId(menuList,0);
    }

    @Override
    public List<Map> findMenuByUser(String username) {
        List<Menu> menuList = menuDao.findMenuByUser(username);
        return findMenuListByParentId(menuList,0);
    }

    private List<Map> findMenuListByParentId(List<Menu>  menuList,Integer parentId ){
        List<Map> mapList=new ArrayList<Map>();
        for( Menu menu:menuList  ){
            if(menu.getParentMenuId().equals(parentId) ){
                Map map=new HashMap();
                map.put("path",menu.getId());
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());
                map.put("linkUrl",menu.getLinkUrl());
                map.put("children", findMenuListByParentId(menuList,menu.getId())   );
                mapList.add(map);
            }
        }
        return mapList;
    }

    public List<Menu> findAll(){


        return menuDao.findAll();
    }
}
